<template>
  <div>
    <van-nav-bar title="账号登录" left-arrow @click-left="$router.back()" />
    <van-form @submit="onSubmit">
      <van-field v-model="username" name="username" placeholder="请输入账号" />
      <van-field
        v-model="password"
        type="password"
        name="password"
        placeholder="请输入密码"
      />
      <div style="margin: 16px">
        <van-button block type="info" native-type="submit">登录</van-button>
      </div>
      <div class="link">
        <router-link to="/register">还没有账号，去注册~</router-link>
      </div>
    </van-form>
  </div>
</template>

<script>
import { login } from '@/api/user'
import { setToken } from '@/utils/storage'
export default {
  name: 'login-page',
  data() {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    async onSubmit(values) {
      // console.log('submit', values)
      const res = await login(values)
      console.log(res)
      setToken(res.body.token)
      this.$toast.success('登录成功')
      this.$router.push('/profile')
    }
  }
}
</script>

<style scoped lang="less">
::v-deep .van-index-bar__index {
  margin: 6px 0;
}

::v-deep .van-nav-bar__content {
  background-color: #21b97a;
}

::v-deep .van-nav-bar__title {
  color: #fff;
}

::v-deep .van-icon-arrow-left:before {
  color: #fff;
}

.link {
  color: #666;
  font-size: 14px;
  padding-left: 20px;
  // text-align: left;
  // float: left;

}

.van-button {
  background-color: #21b97a;
  border: 1px solid rgba(0, 0, 0, 0);
}
</style>
